# Author:Zhang Yu

"""
交换机(Exchange)
交换机的功能主要是接收消息并且转发到绑定的队列，交换机不存储消息，在启用ack模式后，交换机找不到队列会返回错误。交换机有四种类型：Direct, topic, Headers and Fanout

Direct：direct 类型的行为是"先匹配, 再投送". 即在绑定时设定一个 routing_key, 消息的routing_key 匹配时, 才会被交换器投送到绑定的队列中去.
Topic：按规则转发消息（最灵活）
Headers：设置 header attribute 参数类型的交换机
Fanout：转发消息到所有绑定队列

"""
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters("127.0.0.1"))
channel = connection.channel()
channel.exchange_declare(exchange="topic_logs",exchange_type='topic')

routing_key = sys.argv[1] if len(sys.argv) >1 else 'anonymous.info'
message = ' '.join(sys.argv[2:]) or 'hello'
channel.basic_publish(exchange='topic_logs',routing_key=routing_key,body=message)
print("[X] sent ",routing_key,message)
connection.close()